package com.tj.jinzao.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tj.jinzao.R;
import com.tj.jinzao.activity.FamilyMemberActivity;
import com.tj.jinzao.activity.GoodsActivity;
import com.tj.jinzao.activity.MyCollectActivity;
import com.tj.jinzao.activity.MyFansActivity;
import com.tj.jinzao.activity.MyFollowActivity;
import com.tj.jinzao.activity.MyHealthCaseActivity;
import com.tj.jinzao.activity.InviteFriendActivity;
import com.tj.jinzao.activity.MyIdentificationActivity;
import com.tj.jinzao.activity.MyMessageActivity;
import com.tj.jinzao.activity.PersonArticleActivity;
import com.tj.jinzao.activity.SettingsActivity;
import com.tj.jinzao.activity.SignActivity;
import com.tj.jinzao.adapter.MeAdapter;
import com.tj.jinzao.base.BaseDataListFragment;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.User;
import com.tj.jinzao.widgets.RoundedImageView;

import butterknife.OnClick;

public class MeFragment extends BaseDataListFragment {

    private RoundedImageView mHeadView;
    private TextView mNickNameView, mVipView, mFollowView, mFansView, mCollectView, mIntroView;
    public static MeFragment newInstance() {
        MeFragment fragment = new MeFragment();
        if (fragment != null) {
            Bundle args = new Bundle(3);
            args.putString(ARG_TAG_ID, "1");
            args.putInt(ARG_LOAD_ID, 1);
            args.putInt(ARG_LAYOUT_RES_ID, R.layout.fragment_me);
            fragment.setArguments(args);
        }
        return fragment;
    }

    @OnClick(R.id.iv_setting)
    void settings() {
        startActivity(new Intent(mContext, SettingsActivity.class));
    }

    @Override
    protected void initData() {
        mData.add(new Pair(R.mipmap.me_ic_message, "我的消息"));
        mData.add(new Pair(R.mipmap.me_ic_identification, "我的认证"));
        mData.add(new Pair(R.mipmap.me_ic_collect, "我的收藏"));
        mData.add(new Pair(R.mipmap.me_ic_shop, "尽早商城"));
        mData.add(new Pair(R.mipmap.me_ic_member, "成员管理"));
        mData.add(new Pair(R.mipmap.me_ic_report, "成员报告"));
        mData.add(new Pair(R.mipmap.me_ic_invite, "邀请好友"));
//        mData.add(new Pair(R.mipmap.me_ic_health, "健康旅程"));
//        mData.add(new Pair(R.mipmap.me_ic_weather, "天气"));
    }

    @Override
    protected void itemClicked(int position, Object data) {
        switch (position) {
            case 0:
                Intent to=new Intent();
                to.setClass(mContext,MyMessageActivity.class);
                to.putExtra("type",1);
                startActivity(to);
                break;
            case 1:
                startActivity(new Intent(mContext, MyIdentificationActivity.class));
                break;
            case 2:
                startActivity(new Intent(mContext, MyCollectActivity.class));
                break;
            case 3:
                startActivity(new Intent(mContext, GoodsActivity.class));
                break;
            case 4:
                FamilyMemberActivity.start(mContext, 0);
                break;
            case 5:
                FamilyMemberActivity.start(mContext, 1);
                break;
            case 6:
                startActivity(new Intent(mContext, InviteFriendActivity.class));
                break;
//            case 7:
//                startActivity(new Intent(mContext, HealthTripActivity.class));
//                break;
        }
    }

    @Override
    protected void createView(LayoutInflater inflater, View view) {
        View headView = inflater.inflate(R.layout.head_me_info, null);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        headView.setLayoutParams(params);
        mHeadView = headView.findViewById(R.id.iv_head);
        mNickNameView = headView.findViewById(R.id.tv_name);
        mVipView = headView.findViewById(R.id.tv_vip);
        mFollowView = headView.findViewById(R.id.tv_follow);
        mFansView = headView.findViewById(R.id.tv_fans);
        mCollectView = headView.findViewById(R.id.tv_collect);
        mIntroView = headView.findViewById(R.id.tv_intro);
        headView.findViewById(R.id.rl_health_layout).setOnClickListener(this);
        headView.findViewById(R.id.rl_sign_layout).setOnClickListener(this);
        mFansView.setOnClickListener(this);
        mHeadView.setOnClickListener(this);
        mCollectView.setOnClickListener(this);
        mFollowView.setOnClickListener(this);
        mListView.addHeaderView(headView);
        setRefreshEnable(false);
    }

    @Override
    protected RecyclerView.Adapter createAdapter() {
        return new MeAdapter(mContext, mData);
    }

    @Override
    protected void requestData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_health_layout:
                Intent to=new Intent();
                to.putExtra("position",0);
                to.putExtra("userId",String.valueOf(user.getId()));
                to.setClass(getActivity(),MyHealthCaseActivity.class);
                startActivity(to);
                break;
            case R.id.rl_sign_layout:
                startActivity(new Intent(mContext, SignActivity.class));
                break;
            case R.id.iv_head:
                startActivity(new Intent(mContext, PersonArticleActivity.class)
                        .putExtra("user", user));
                break;
            case R.id.tv_follow:
                startActivity(new Intent(mContext, MyFollowActivity.class));
                break;
            case R.id.tv_fans:
                startActivity(new Intent(mContext, MyFansActivity.class));
                break;
            case R.id.tv_collect:
                startActivity(new Intent(mContext, MyCollectActivity.class));
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        bindDataView();
    }
    User user;
    private void bindDataView() {
        user = UserManager.getInstance().getUser();
        Glide.with(mContext).load(Constant.IMAGE_PREFIX + user.getImgurl()).into(mHeadView);
        mNickNameView.setText((TextUtils.isEmpty(user.getNickname()) ? "" : user.getNickname()));
        mVipView.setText("V" + user.getLevel());
        int follow_size = 0;
        int fans_size = 0;
        int praise_size = 0;
        if(user.getUserCircleStatistics() != null){
            follow_size = user.getUserCircleStatistics().getFollowCount();
            fans_size = user.getUserCircleStatistics().getFansCount();
            praise_size = user.getUserCircleStatistics().getLikeCount();
        }
        mFollowView.setText("关注" + follow_size);
        mFansView.setText("粉丝" + fans_size);
        mCollectView.setText("获赞与收藏" + praise_size);
        mIntroView.setText(TextUtils.isEmpty(user.getSignature()) ? "一句话介绍…" : user.getSignature());
    }

    @Override
    protected void setEmptyDataView() {

    }

    @Override
    protected boolean resolveData(Object o) {
        return false;
    }

    @Override
    public void onRequestNoData(String message) {

    }

    @Override
    protected boolean canLoadMoreItems() {
        return false;
    }

}
